<?php
if (eregi("session.php",$_SERVER['SCRIPT_NAME'])) {
    Header("Location: ../index.php");
    die();
}

class Session {
// VARS:
// $id        = string
// $language  = string
// $ip        = string
// $refresh   = int
// $timelimit = int
//
// METHODS:
// init ()
// getVisitor (string)


    // settings
	var $id        = "";
    var $language  = "";
	var $ip        = "";
	var $refresh   = 0;
	var $timelimit = 3600; // time limit to delete the user from the database


    // {{{ init()
    //
    // initialize the session and save the visitor data into the database if they don't exist.
    //
    // IN: -
    // OUT: -
    //
    function init() {
	    global $db, $table, $PHPSESSID;
		
		    // delete all database entries where refresh is too old
		    $delete = time()-$this->timelimit;
		    $db->query("DELETE FROM $table->online WHERE refresh<'$delete'");
        
	    session_start();
		// save the visitor data into the database if they don't exist
		$this->id = md5($PHPSESSID);
		$time = time();
		if ($db->numRows($db->query("SELECT sessid FROM $table->online WHERE sessid='$this->id' LIMIT 0,1")) == 0)
		{
		    $db->query("INSERT INTO $table->online VALUES ('$this->id','".GLOBAL_IP."','','$time','$time')");
			$this->ip       = GLOBAL_IP;
			$this->refresh  = $time;
		}
		else { $this->getVisitor($this->id); }
    }
	// }}


	// {{{ getVisitor()
    //
    // get the visitor data by his session ID.
    //
    // IN: string [$sessid: session ID of the user]
    // OUT: -
    //
    function getVisitor($sessid) {
	    global $db, $table;
		$result = $db->query("SELECT * FROM $table->online WHERE sessid='$sessid' LIMIT 0,1");
		if ($db->numRows($result) > 0) { // a visitor with this session ID does exist
		    $time = time();
		    $db->query("UPDATE $table->online SET refresh='$time' WHERE sessid='$this->id'"); // update the refresh time
			$visitor = $db->nextRecord($result);
			$this->language = $visitor->language;
			$this->ip       = $visitor->ip;
			$this->refresh  = $visitor->time;
		}
    }
	// }}


	// {{{ create()
    //
    // create a session var.
    //
    // IN: string ($name: name)
    // OUT: -
    //
    function create($name, $value = NULL) {
    	//debug_print_backtrace();
	    //session_register($name);
	    $_SESSION[$name] = $value;
    }
	// }}


	// {{{ destroy()
    //
    // destroy a session var.
    //
    // IN: string ($name: name)
    // OUT: -
    //
    function destroy($name) {
	    //session_unregister($name);
    	unset($_SESSION[$name]);
    }
	// }}


} // end class Session
?>